<template>
  <div class="home-container">
    <!-- 搜索按钮 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button round class="search-btn" type="default" slot="title" icon="search" to="/search">搜索</van-button>
    </van-nav-bar>
    <!-- 频道列表 -->
    <van-tabs v-model="active" animated swipeable class="channel-tabs">
      <van-tab :title="obj.name" v-for="obj in UserChannelsList" :key="obj.id">
        <!-- 文章列表组件 -->
        <articleList :channels="obj"></articleList>
      </van-tab>
      <!-- 占位的盒子 -->
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="isShowPopup = true">
        <van-icon name="wap-nav" />
      </div>
    </van-tabs>
    <van-popup
    close-icon-position="top-left"
    closeable v-model="isShowPopup"
    position="bottom"
    :style="{ height: '90%' }"
    >
      <channelEdit :channels="UserChannelsList" @updateActive="updateActive" :active="active"></channelEdit>
    </van-popup>
  </div>
</template>

<script>
// 获取用户频道列表
import { getUserChannels } from '@/api/user'
// 频道编辑弹出层组件
import channelEdit from '@/views/home/components/channel-edit'
// 文章列表组件
import articleList from '@/views/home/components/article-list'
import { mapState } from 'vuex'
import { getItem } from '@/utils/stroage'
export default {
  name: 'HomeIndex',
  data () {
    return {
      active: 0, // 控制被激活的标签
      UserChannelsList: [], // 用户频道列表
      isShowPopup: false // 控制频道列表弹出层
    }
  },
  methods: {
    // 获取用户频道列表
    async getUserChannels () {
      const localChannel = getItem('toutiao-channel')
      // 判断是否登录 如果没有登录且本地存储数据不为空的话
      if (!this.user && localChannel) {
        this.UserChannelsList = localChannel
      } else {
        try {
          const res = await getUserChannels()
          const { data } = res
          this.UserChannelsList = data.data.channels
        } catch (err) {
          console.log(err)
        }
      }
    },
    // item组件点击切换频道
    updateActive (index, isShowPopup = true) {
      this.active = index
      this.isShowPopup = isShowPopup
    }
  },
  created () {
    this.getUserChannels()
  },
  components: {
    articleList,
    channelEdit
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-bottom: 100px;
  padding-top: 176px;
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    color: #fff;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  // 深度操作符 可以影响所有的后代元素
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }

    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777777;
    }

    .van-tab--active {
      color: #333333;
    }

    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }

    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }

    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      background-color: rgba(255, 255, 255, 0.902);
      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 58px;
        // background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
